package iface.aplicativo.filtro;

import iface.aplicativo.negocio.entidade.Menu;
import iface.aplicativo.to.UsuarioLogadoTO;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class UsuarioFilter
 */
public class UsuarioFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public UsuarioFilter() {
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;

		HttpSession session = req.getSession(false);

		boolean isPagina = (req.getRequestURI().indexOf("login") == -1
				&& req.getRequestURI().indexOf("javax.faces.resource") == -1
				&& req.getRequestURI().indexOf("/css/") == -1
				&& req.getRequestURI().indexOf("/img/") == -1 && req
				.getRequestURI().indexOf("/js/") == -1);

		if (isPagina) {
			if (session != null) {

				String urlUltimaPagina = (String) session
						.getAttribute("ultima_pagina_acessada");
				if (urlUltimaPagina != null
						&& !req.getRequestURI().equals(urlUltimaPagina)) {
					Enumeration<?> e = session.getAttributeNames();
					while (e.hasMoreElements()) {
						String nomeAttri = (String) e.nextElement();
						if (nomeAttri != "userLoggedIn") {
							session.removeAttribute(nomeAttri);
						}
					}
				}

				session.setAttribute("ultima_pagina_acessada",
						req.getRequestURI());

				UsuarioLogadoTO usuario = (UsuarioLogadoTO) session
						.getAttribute("userLoggedIn");

				if ((usuario == null)) {
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/login.jsf");
					dispatcher.forward(request, response);
				} else if (!usuario.getMenus().isEmpty()) {
					boolean temAcesso = false;
					for (Menu menu : usuario.getMenus()) {
						if ((menu.getIdMenuPai() != null || (menu
								.getIdMenuPai() == null && menu.getFilhos()
								.isEmpty()))
								&& (menu.getUrlPasta() != null && req
										.getRequestURI().indexOf(
												menu.getUrlPasta()) > -1)) {
							temAcesso = true;
							break;
						}
					}

					if (temAcesso) {
						chain.doFilter(request, response);
					} else {
						RequestDispatcher dispatcher = request
								.getRequestDispatcher("/sempermissao.jsf");
						dispatcher.forward(request, response);
					}
				} else {
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/sempermissao.jsf");
					dispatcher.forward(request, response);
				}
			} else {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/login.jsf");
				dispatcher.forward(request, response);
			}
		} else {
			chain.doFilter(request, response);
		}

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
